const path = require("path");
const webpack = require("webpack");
const nodeExternals = require("webpack-node-externals");
const StartServerPlugin = require("start-server-webpack-plugin");

const serverConfig = {
  mode: "development",
  target: "node",
  watch: true,
  entry: ["webpack/hot/signal", path.resolve(__dirname, "../scripts/start")],
  output: {
    path: path.resolve(__dirname, "../dist"),
    filename: "server.js"
  },
  watchOptions: {
    ignored: /node_modules/,
  },
  module: {
    rules: [
      {
          test: /\.ts$/,
          enforce: 'pre',
          use: [
              {
                  loader: 'tslint-loader',
                  options: { /* Loader options go here */ }
              }
          ]
      },
      {
        test: /\.ts$/,
        use: [
          {
            loader: 'babel-loader',
          },
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
              configFile: path.resolve(__dirname, './tsconfig.json'),
            },
          },
        ],
        exclude: /node_modules/,
      }
    ]
  },
  externals: [
    nodeExternals({
      whitelist: ["webpack/hot/signal"]
    })
  ],
  resolve: {
    extensions: [".ts", ".js", ".json"]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new StartServerPlugin({
      name: "server.js",
      signal: true,
      nodeArgs: ["--inspect"]
    })
  ]
};

module.exports = serverConfig;
